package io.github.xbeeant.eoffice.service.impl;

import io.github.xbeeant.core.ApiResponse;
import io.github.xbeeant.core.IdWorker;
import io.github.xbeeant.core.JsonHelper;
import io.github.xbeeant.eoffice.config.AbstractSecurityMybatisPageHelperServiceImpl;
import io.github.xbeeant.eoffice.mapper.FolderUserMapper;
import io.github.xbeeant.eoffice.model.FolderUser;
import io.github.xbeeant.eoffice.model.User;
import io.github.xbeeant.eoffice.rest.vo.FolderUserVo;
import io.github.xbeeant.eoffice.service.IFolderUserService;
import io.github.xbeeant.eoffice.service.IUserService;
import io.github.xbeeant.spring.mybatis.antdesign.PageResponse;
import io.github.xbeeant.spring.mybatis.pagehelper.IMybatisPageHelperDao;
import io.github.xbeeant.spring.mybatis.pagehelper.PageBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * eoffice_folder_user
 */
@Service
public class FolderUserServiceImpl extends AbstractSecurityMybatisPageHelperServiceImpl<FolderUser, Long> implements IFolderUserService {

    @Autowired
    private FolderUserMapper folderUserMapper;

    @Autowired
    private IUserService userService;

    @Override
    public ApiResponse<PageResponse<FolderUserVo>> fuzzySearchVoByPager(FolderUser record, PageBounds pageBounds) {
        ApiResponse<PageResponse<FolderUserVo>> result = new ApiResponse<>();
        ApiResponse<PageResponse<FolderUser>> apiResponse = fuzzySearchByPager(record, pageBounds);
        if (apiResponse.getSuccess()) {
            PageResponse<FolderUserVo> pageResponse = new PageResponse<>();
            pageResponse.setPagination(apiResponse.getData().getPagination());
            FolderUserVo folderUserVo;
            for (FolderUser folderUser : apiResponse.getData()) {
                folderUserVo = JsonHelper.toObject(JsonHelper.toJsonString(folderUser), FolderUserVo.class);
                ApiResponse<User> userApiResponse = userService.selectByPrimaryKey(folderUser.getUid());
                if (userApiResponse.getSuccess()) {
                    folderUserVo.setUsername(userApiResponse.getData().getNickname());
                }
                pageResponse.add(folderUserVo);
            }
            result.setData(pageResponse);
        }
        return result;
    }

    @Override
    public IMybatisPageHelperDao<FolderUser, Long> getRepositoryDao() {
        return this.folderUserMapper;
    }

    @Override
    public void setDefaults(FolderUser record) {
        if (record.getId() == null) {
            record.setId(IdWorker.getId());
        }
    }
}
